package com.TicTacToe;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TicTacToe2PlayerActivity extends Activity implements View.OnClickListener {

	private TicTacToeGame Game;
	private Button btn[];
	private Button restart;
	private TextView Human1_num,Human2_num,Draw_num,Human1TXT,Human2TXT,DrawTXT,Turns;
	private boolean gOver = false;
	
	private int Human1_counter = 0 , Human2_counter = 0 , Draw_counter = 0 ;
	private boolean Human1First = true ;
	private char playerId =Game.Human1;
	
	 @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.main);
	        // find views
	        
	        Human1_num = (TextView) findViewById(R.id.Human_num);
	        Human2_num = (TextView) findViewById(R.id.Computer_num);
	        Draw_num = (TextView) findViewById(R.id.Draw_num);
	        Human1TXT = (TextView) findViewById(R.id.HumanTXT);
	        Human2TXT= (TextView) findViewById(R.id.ComputerTXT);
	        DrawTXT = (TextView) findViewById(R.id.DrawTXT);
	        Turns = (TextView) findViewById(R.id.Turns);
	        
	        
	        btn = new Button[Game.getBoard_Size()];
	        
	        findViewById(R.id.AbsoluteLayout1);
	        btn[0] = (Button) findViewById(R.id.btn1);
	        btn[1] = (Button) findViewById(R.id.btn2);
	        btn[2] = (Button) findViewById(R.id.btn3);
	        btn[3] = (Button) findViewById(R.id.btn4);
	        btn[4] = (Button) findViewById(R.id.btn5);
	        btn[5] = (Button) findViewById(R.id.btn6);
	        btn[6] = (Button) findViewById(R.id.btn7);
	        btn[7] = (Button) findViewById(R.id.btn8);
	        btn[8] = (Button) findViewById(R.id.btnlast);
	       restart = (Button) findViewById(R.id.restart);
	        
	        
	        Human1_num.setText(Integer.toString(Human1_counter));
	        Human2_num.setText(Integer.toString(Human2_counter));
	        Draw_num.setText(Integer.toString(Draw_counter));
	        Human1TXT.setText("player(1) :");
	        Human2TXT.setText("player(2) :");
	        
	        
	     // setting a new action for the restart button 
			
			restart.setOnClickListener(new View.OnClickListener() {
				
		// 	making the button unclickable in if the game didn't end
				
			public void onClick(View v) {
				if(!gOver){
					
					restart.setOnClickListener(null);
					
				}else{
					
					// restarting the game after it ends
					
				gOver = false;
				Game = new TicTacToeGame();
				startGame();
				
				}
			}
		});
	        
	        //create the game
	        
	        Game = new TicTacToeGame(); 
	        
	        startGame();
	    }
	 
	 
	 public void setMove(char player, int location) {
			//setting the values of the buttons
			Game.setMove(player, location);
			btn[location].setEnabled(false);
			btn[location].setText(String.valueOf(player));
			//colors of the marks
			if(player == Game.Human1){
				btn[location].setTextColor(Color.CYAN);
			}else if(player == Game.Human2){
				btn[location].setTextColor(Color.RED);
			}
		}	
	 
	 
	 private void startGame(){
     	//clear the board
     	Game.clearBoard();
     	for(int i = 0;i < btn.length;i++){
     		btn[i].setText("");
     		btn[i].setEnabled(true);
     		btn[i].setOnClickListener(this);
     		
     	}
     	//setting who goes first
     	if(Human1First){
     		// human 1
     		Turns.setText("player 1 is first");
     		Human1First = false;
     	}else{
     		// Human 2
     		Turns.setText("player 2 is first");
     		Human1First = true;
     	}
     	
     }
	 
	
		
		    	//what happens when the buttons are clicked
		
		
			public void onClick(View v) {
				
				
				
				//
				
				// declaring the location and values to be assigned for the clicked buttons
				int location=-1;
				int id = v.getId();
				
				// assigning the values for button clicked
				
				switch(id){	
				
				//the location of each button
								
				case R.id.btn1:
					location=0;
					break;
				case R.id.btn2:
					location=1;
					break;
				case R.id.btn3:
					location=2;
					break;
				case R.id.btn4:
					location=3;
					break;
				case R.id.btn5:
					location=4;
					break;
				case R.id.btn6:
					location=5;
					break;
				case R.id.btn7:
					location=6;
					break;
				case R.id.btn8:
					location=7;
					break;
				case R.id.btnlast:
					location=8;
					break;
		
				}
				
				//checking if the game was over
				
				if(!gOver){
					
					//if the value of a button is still empty continue to play and after the move check for a winner
					
	        		if(btn[location].isEnabled()){
	        			Turns.setText("player 1 turn");
	        			setMove(playerId, location);
	        			int winner = Game.checkForWinner2player();
	        			if(winner == 0 ){
	        				if (playerId==Game.Human1){
	        				Turns.setText("player 2 turn");
	        				playerId = Game.Human2;}
	        				else if(playerId==Game.Human2){
	        					Turns.setText("player 1 turn");
	        					playerId = Game.Human1;
	        				}
	        				winner = Game.checkForWinner2player();
	        			}
	        			
	        			//if there is an empty button continue to play
	        			
	        			if(winner == 0 ){
	        				if(playerId == Game.Human1){
	        				Turns.setText("player(1) turn");
	        				}else{
	        				Turns.setText("player(2) turn");
	        				}
	        				
	        			// if it was a draw
	        				
	        			}else if(winner ==1){
	        				Turns.setText("its a draw");
	        				Draw_counter++;
	        				Draw_num.setText(Integer.toString(Draw_counter));
	        				gOver = true;

	        		
	        				
	        				// if the you win
	        				
	        			}else if(winner ==2){
	        				Turns.setText("player 1 wins");
	        				Human1_counter++;
	        				Human1_num.setText(Integer.toString(Human1_counter));
	        				gOver = true;      		
	        				
	        				// if the computer wins
	        				
	        			}else{
	        				Turns.setText("player 2 wins");
	        				Human2_counter++;
	        				Human2_num.setText(Integer.toString(Human2_counter));
	        				gOver = true;

	        			}	        				
	        		}			        			
				}				
			}
						
}
